#!/usr/bin/env python

import sys, os
import time
from numpy import *
from numpy.linalg import svd

nargs=len(sys.argv)

if nargs<2:
   print 'Usage: '+sys.argv[0]+' file'
   sys.exit(-1)

filename=sys.argv[1]

print 'Loading matrix from file: ', filename
mmdata=io.read_array(filename)
nrows,ncols=mmdata.shape

print 'Processing SVD of a %d x %d matrix ... ' % (nrows, ncols)
t1 = time.time()
bb=svd(mmdata)
t2 = time.time()
print 'For a %d x %d matrix it took %0.3f sec' % (nrows, ncols, t2-t1)

del mmdata
io.write_array('svd_u.txt', bb[0])
io.write_array('svd_s.txt', bb[1])
io.write_array('svd_v.txt', bb[2])

if nrows<20 and ncols<10:
  print '- '*20
  os.system('cat svd_u.txt')
  print '- '*20
  os.system('cat svd_s.txt')
  print '- '*20
  os.system('cat svd_v.txt')
  print '- '*20
